﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 工作组岗位DAL
    /// </summary>
    public class HRWorkGrpDuty
    {
        /// <summary>
        /// 获取数据列表
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>工作组岗位数据列表</returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FEntityNo,FWorkGrpNo,FDutyProp,FWorkGrpDutyNo,FSeq,FDutyName ");
            strSql.Append(" FROM t_HRWorkGrpDuty ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fWorkGrpDutyNo">工作组岗位编号</param>
        public void Delete(string fEntityNo, string fWorkGrpNo, string fWorkGrpDutyNo)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_HRWorkGrpDuty ");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FWorkGrpDutyNo=@FWorkGrpDutyNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpDutyNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fWorkGrpNo;
            parameters[2].Value = fWorkGrpDutyNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //执行存储过程，生成临时表数据，以便查询时调用 
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("EXEC sp_genDocAuthorityDW @EntityNo");
            SqlParameter[] parameters2 = {
					new SqlParameter("@EntityNo", SqlDbType.NVarChar,12)};
            parameters2[0].Value = fEntityNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }

        /// <summary>
        /// 是否存在该记录
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fWorkGrpDutyNo">工作组岗位编号</param>
        /// <returns>工作组岗位数据列表</returns>
        public bool Exists(string fEntityNo, string fWorkGrpNo, string fWorkGrpDutyNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_HRWorkGrpDuty");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FWorkGrpDutyNo=@FWorkGrpDutyNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpDutyNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fWorkGrpNo;
            parameters[2].Value = fWorkGrpDutyNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 获得数据列表
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="row">需转换数据源</param>
        /// <returns>转换后数据信息</returns>
        public ElingOA6_Admin_DSJCJ.Model.HRWorkGrpMember DataRowToHRWorkGrpMemberModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.HRWorkGrpMember model = new ElingOA6_Admin_DSJCJ.Model.HRWorkGrpMember();
            if (row != null)
            {
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FWorkGrpNo"] != null)
                {
                    model.FWorkGrpNo = row["FWorkGrpNo"].ToString();
                }
                if (row["FWorkGrpName"] != null)
                {
                    model.FWorkGrpName = row["FWorkGrpName"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FWorkDuty"] != null)
                {
                    model.FWorkDuty = row["FWorkDuty"].ToString();
                }
                if (row["FSeq"] != null && row["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(row["FSeq"].ToString());
                }
                if (row["FEmpName"] != null)
                {
                    model.FEmpName = row["FEmpName"].ToString();
                }
                if (row["FDutyName"] != null)
                {
                    model.FDutyName = row["FDutyName"].ToString();
                }
                if (row["FWorkGrpDutyNo"] != null)
                {
                    model.FWorkGrpDutyNo = row["FWorkGrpDutyNo"].ToString();
                }
                if (row["FDutyProp"] != null)
                {
                    model.FDutyProp = row["FDutyProp"].ToString();
                }
            }
            return model;
        }

    }
}
